package com.bqtv.sport.ui.main.fragment

import android.content.Context
import android.graphics.Typeface
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewbinding.ViewBinding
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.bqtv.sport.R
import com.bqtv.sport.app.AppActivity
import com.bqtv.sport.app.AppFragment
import com.bqtv.sport.commen.widget.CustomPagerTitleView
import com.bqtv.sport.databinding.FragmentMatchBinding
import com.bqtv.sport.event.MatchFragmentEvent
import com.bqtv.sport.ui.match.MatchSettingActivity
import com.bqtv.sport.ui.match.fragment.MatchAuthorFragment
import com.bqtv.sport.ui.match.fragment.MatchFollowFragment
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class MatchFragment : AppFragment<FragmentMatchBinding, AppActivity<ViewBinding>>() {
    private var mTitles: MutableList<String> = ArrayList ()
    private var mViewList: MutableList<Fragment> = ArrayList ()
    companion object {
        fun newInstance(): MatchFragment {
            val fragment = MatchFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun getViewBinding() = FragmentMatchBinding.inflate(layoutInflater)

    override fun initView() {
        EventBus.getDefault().register(this)

    }

    override fun initData() {
        mTitles.add(getString(R.string.follow))
        mTitles.add(getString(R.string.author))
        mTitles.add(getString(R.string.hot))
        mTitles.add(getString(R.string.football))
        mTitles.add(getString(R.string.basketball))
        mTitles.add(getString(R.string.match_result))

        mViewList.add(MatchFollowFragment.newInstance())
        mViewList.add(MatchAuthorFragment.newInstance())
        mViewList.add(MatchAuthorFragment.newInstance())
        mViewList.add(MatchAuthorFragment.newInstance())
        mViewList.add(MatchAuthorFragment.newInstance())
        mViewList.add(MatchAuthorFragment.newInstance())
        initViewPager()

    }

    private fun initViewPager() {
        //初始化指示器
        val commonNavigator = CommonNavigator(getContext())
        commonNavigator.setAdapter(object : CommonNavigatorAdapter() {
            override fun getCount(): Int {
                return mTitles.size
            }

            override fun getTitleView(context: Context?, index: Int): IPagerTitleView {
                val titleView: CustomPagerTitleView = CustomPagerTitleView(context)
                titleView.setNormalColor(getResources().getColor(R.color.text_222222))
                titleView.setSelectedColor(getResources().getColor(R.color.text_222222))
                titleView.setText(mTitles.get(index))
                titleView.setTextSize(18f)
                //                titleView.getPaint().setFakeBoldText(true);
                titleView.setOnPagerTitleChangeListener(object : CustomPagerTitleView.OnPagerTitleChangeListener {
                    public override fun onSelected(index: Int, totalCount: Int) {
                        titleView.setTextSize(18f)
                        titleView.setTypeface(null, Typeface.BOLD)
                    }

                    public override fun onDeselected(index: Int, totalCount: Int) {
                        titleView.setTextSize(14f)
                        titleView.setTypeface(null, Typeface.NORMAL)
                    }

                    public override fun onLeave(
                        index: Int,
                        totalCount: Int,
                        leavePercent: Float,
                        leftToRight: Boolean
                    ) {
                    }

                    public override fun onEnter(
                        index: Int,
                        totalCount: Int,
                        enterPercent: Float,
                        leftToRight: Boolean
                    ) {
                    }
                })
                titleView.setOnClickListener(object : View.OnClickListener {
                    override fun onClick(v: View?) {
                        if (binding.viewpager != null) {
                            binding.viewpager.setCurrentItem(index)
                        }
                    }
                })
                return titleView
            }

            override fun getIndicator(context: Context): IPagerIndicator? {
                val linePagerIndicator = LinePagerIndicator(context)
                 return null
            }
        })
        commonNavigator.setAdjustMode(false)
        //初始化viewpager
        binding.viewpager.addOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(i: Int, v: Float, i1: Int) {
            }

            override fun onPageSelected(i: Int) {
            }

            override fun onPageScrollStateChanged(i: Int) {
            }
        })
        binding.viewpager.setAdapter(object : FragmentPagerAdapter(getChildFragmentManager()) {
            override fun getItem(i: Int): Fragment {
                return mViewList.get(i)
            }

            override fun getCount(): Int {
                return mViewList.size
            }
        })
        binding.magicIndicator.setNavigator(commonNavigator)
        ViewPagerHelper.bind(binding.magicIndicator, binding.viewpager)

        initListener()
    }

    private fun initListener() {
        binding.ivSetting.setOnClickListener(this)
    }

    override fun onClick(view: View) {
        super.onClick(view)
        when (view.id) {
            R.id.iv_setting -> {
                startActivity(MatchSettingActivity::class.java)
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMatchIndexEvent(event: MatchFragmentEvent?) {
        if (event != null) {
                binding.viewpager.setCurrentItem(event.index)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
    }
}

